package com.iseastar.guojiang.order;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.CabinetBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.StationParcelBean;
import com.shouhuobao.station.R;

/**
 * 订单、异常申报
 *
 * @author coffee <br>
 *         2016-10-8下午5:04:36
 */
public class OrderExceptionActivity extends BaseActivity2 implements OnClickListener {
	// 拒收原因
	private EditText mReason;

	private RadioGroup mRadioGroup;
	private RadioButton[] mExceptionItems;// 异常

	private StationParcelBean item;

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		item = (StationParcelBean) getIntent().getSerializableExtra("item");
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.order_exception);
		super.findViewById();
		getAppTitle().setCommonTitle("选择异常原因");
		mReason = (EditText) findViewById(R.id.order_reason);
		findViewById(R.id.submit).setOnClickListener(this);
		mRadioGroup = (RadioGroup) findViewById(R.id.order_radiogroup);
		mExceptionItems = new RadioButton[mRadioGroup.getChildCount()];
		for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
			mExceptionItems[i] = (RadioButton) mRadioGroup.getChildAt(i);
			mExceptionItems[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// 其他
					if (buttonView.getId() == mExceptionItems[mRadioGroup.getChildCount() - 1].getId()) {
						if (isChecked) {
							mReason.setVisibility(View.VISIBLE);
						} else {
							mReason.setVisibility(View.INVISIBLE);
						}
					}
				}
			});
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.submit:
				int checkedId = mRadioGroup.getCheckedRadioButtonId();
				// 未选中 | 或者选中的是"其他" 但是未填写原因
				if (checkedId == -1 || (checkedId == mExceptionItems[mRadioGroup.getChildCount() - 1].getId() && isEmpty(mReason.getText()))) {
					showToast("请输入异常现象");
					return;
				}
				RadioButton radio = (RadioButton) mRadioGroup.findViewById(checkedId);
				String reason = radio.getText().toString();
				// 选的是其他
				if (checkedId == mExceptionItems[mRadioGroup.getChildCount() - 1].getId()) {
					reason = mReason.getText().toString();
				}
				showLoadingDialog(null);
				CabinetBean.StationCabubetBean click = AppCache.getCabinetOrStation();
				AppHttp.getInstance().orderException(item.getParcelId(), click.getId(), click.getPhone(), item.getBagCode(), reason);
				break;
			default:
				break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			// 订单异常申报
			case MsgID.order_exception:
				cancelLoadingDialog();
				ReqResult<?> result = JSON.parser(msg.obj);
				if (checkLoginStatus(result)) {
					showToast("操作成功");
					setResult(RESULT_OK);
					finish();
				} else {
					showToast(result.getMessage());
				}
				return true;
			default:
				return super.handleMessage(msg);
		}
	}
}
